﻿Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class CustoFatura
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Faz com que ao sair do aplicativo, limpe o buffer e não podendo entrar na página através do butão voltar do Browser.
        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        Try
            'If Not Session("Logged") Then
            '    Session("ShowSessionExpired") = 1 ' Show lblSessionExpired in Login.aspx
            '    Response.Redirect("Login.aspx")
            'End If

            If Not Page.IsPostBack Then


                ' Header
                lblModulo.Text = "Administrador"
                lblModuloItem.Text = "Cadastro de Fatura"

                btnIncluir.Enabled = False
                BtnConsultar.Enabled = False
                btnAlterar.Enabled = False
                btnExcluir.Enabled = False
                btnItem.Enabled = False

                Dim strConnection As String
                strConnection = "A"

                If (LoadHolding(strConnection) = True) Then

                    FillDDL()
                    LoadData()
                    'ddlSituacao.SelectedIndex = 2 ' Parcial

                    If (CarregaGVW() = True) Then
                        btnIncluir.Enabled = True
                    End If

                End If

            End If

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    ' Step 02
    Private Function LoadHolding(ByVal strConn As String) As Boolean

        LoadHolding = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim theQueryString As String = "SELECT CodigoDaHolding, NomeDaHolding" & _
                         " FROM Holding;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                If (theSqlDataReader.HasRows = True) Then
                    ' Loop through all the rows in the DataTableReader.
                    Do While theSqlDataReader.Read()
                        lblHolding.Text = theSqlDataReader(1).ToString
                        lblHolding.ToolTip = theSqlDataReader(0).ToString
                    Loop

                    LoadHolding = True

                Else
                    lblMsg.Text = "Holding não cadastrada no Sistema."
                End If

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    Private Sub FillDDL()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDaCia, CodigoDaCia + ' - ' + NomeDaCia AS Cia" & _
                     " FROM Companhia" & _
                     " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " ORDER BY CodigoDaCia;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlCompanhia.DataTextField = "Cia"
                ddlCompanhia.DataValueField = "CodigoDaCia"
                ddlCompanhia.DataSource = mydatatable
                ddlCompanhia.DataBind()

                ddlCompanhia.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using


        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodSituacaoFatura, DscSituacaoFatura" & _
                 " FROM SituacaoFatura" & _
                 " ORDER BY CodSituacaoFatura;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)

                ddlSituacao.DataTextField = "DscSituacaoFatura"
                ddlSituacao.DataValueField = "CodSituacaoFatura"
                ddlSituacao.DataSource = mydatatable
                ddlSituacao.DataBind()

                ddlSituacao.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodFornecedor, RazaoSocialFornecedor" & _
                         " FROM Fornecedor" & _
                         " ORDER BY RazaoSocialFornecedor;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlFornecedor.DataTextField = "RazaoSocialFornecedor"
                ddlFornecedor.DataValueField = "CodFornecedor"
                ddlFornecedor.DataSource = mydatatable
                ddlFornecedor.DataBind()

                ddlFornecedor.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Sub FillDDLEstabelecimento()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodigoDoEstabelecimento" & _
                     " FROM Estabelecimento" & _
                     " WHERE CodigoDaHolding = '" & lblHolding.ToolTip & "'" & _
                     " AND CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'" & _
                     " ORDER BY CodigoDoEstabelecimento;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                ddlEstabelecimento.DataTextField = "CodigoDoEstabelecimento"
                ddlEstabelecimento.DataValueField = "CodigoDoEstabelecimento"
                ddlEstabelecimento.DataSource = mydatatable
                ddlEstabelecimento.DataBind()

                ddlEstabelecimento.Items.Insert(0, "")

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Private Sub LoadData()

        'Dim br As New CultureInfo("pt-BR")

        Dim intQtdeItems As Integer = 0
        'Dim strFatura As String

        ddlCompanhia.Enabled = False
        ddlEstabelecimento.Enabled = False

        If (Session("strOperacao").ToString = "C" Or Session("strOperacao").ToString = "E") Then ' Consultar | Excluir
            'txtFatura.Enabled = False
        End If

        lblHolding.Text = Session("strHolding")
        lblHolding.ToolTip = Session("strCodigoDaHolding")

        ddlCompanhia.SelectedValue = Session("strCodigoDaCia").ToString
        FillDDLEstabelecimento()
        ddlEstabelecimento.SelectedValue = Session("strCodigoDoEstabelecimento").ToString
        txtNumeroDocumento.Text = Session("strNumeroDocumento")

 



    End Sub

    Private Function CarregaGVW() As Boolean

        Dim strWhere As String = ""
        'Dim strFatura As String

        CarregaGVW = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT A.CodigoDaCia, A.CodigoDoEstabelecimento, A.NumeroDocumento, A.Fatura, A.DataFatura, B.DscSituacaoFatura, C.SiglaDaMoeda, A.ValorTotalFatura, A.ValorDespesasEmbarque, D.RazaoSocialFornecedor" & _
                     " FROM CustoFaturaHeader A, situacaofatura B, moeda C, fornecedor D"

            If Not (ddlCompanhia.Text = "") Then
                strWhere = " AND A.CodigoDaCia = '" & ddlCompanhia.SelectedValue & "'"
            End If

            If Not (ddlEstabelecimento.Text = "") Then
                strWhere = strWhere & _
                           " AND A.CodigoDoEstabelecimento = '" & ddlEstabelecimento.SelectedValue & "'"
            End If

            If Not (Trim(txtNumeroDocumento.Text) = "") Then
                If (strWhere = "") Then
                    strWhere = " AND A.NumeroDocumento LIKE '%" & txtNumeroDocumento.Text & "%'"
                Else
                    strWhere = strWhere & _
                               " AND A.NumeroDocumento LIKE '%" & txtNumeroDocumento.Text & "%'"
                End If
            End If

            If Not (ddlSituacao.Text = "") Then
                If (strWhere = "") Then
                    strWhere = " AND A.CodSituacaoFatura = " & ddlSituacao.SelectedValue
                Else
                    strWhere = strWhere & _
                               " AND A.CodSituacaoFatura = " & ddlSituacao.SelectedValue
                End If
            End If

            If Not (ddlFornecedor.Text = "") Then
                If (strWhere = "") Then
                    strWhere = " AND A.CodFornecedor = '" & ddlFornecedor.SelectedValue & "'"
                Else
                    strWhere = strWhere & _
                               " AND A.CodFornecedor = '" & ddlFornecedor.SelectedValue & "'"
                End If
            End If

            'If Not (strWhere = "") Then
            theQueryString = theQueryString & _
                     " WHERE A.CodSituacaoFatura = B.CodSituacaoFatura AND A.CodigoDaMoeda = C.CodigoDaMoeda AND A.CodFornecedor = D.CodFornecedor AND NumeroDocumento = " & Session("strNumeroDocumento") & "" & strWhere
            'End If

            theQueryString = theQueryString & _
                     " ORDER BY A.CodigoDaCia, A.CodigoDoEstabelecimento, A.DataFatura DESC, A.Fatura DESC;"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                'If (theSqlDataReader.HasRows = True) Then
                '    strFatura = theSqlDataReader!Fatura
                'End If

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()

                CarregaGVW = True

                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Function

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        Try
            CarregaGVW()
            gvwData.PageIndex = e.NewPageIndex
            gvwData.DataBind()
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        Dim strSituacaoFatura As String = ""

        Try

            BtnConsultar.Enabled = True
            btnAlterar.Enabled = True
            btnExcluir.Enabled = True
            btnItem.Enabled = True

            strSituacaoFatura = HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text)

            If (strSituacaoFatura = "Parcial" Or strSituacaoFatura = "Faturada") Then
                btnAlterar.Enabled = True
                btnExcluir.Enabled = True
            End If

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub



    Protected Sub btnPesquisar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPesquisar.Click

        CarregaGVW()

    End Sub

    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Vazio

        Session.Add("strHolding", lblHolding.Text)
        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", ddlCompanhia.SelectedValue)
        Session.Add("strCodigoDoEstabelecimento", ddlEstabelecimento.SelectedValue)
        'Session.Add("strCodFornecedorServico", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(9).Text))
        Session.Add("strNumeroDocumento", txtNumeroDocumento.Text)
        'Session.Add("strDataDocumento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))
        'Session.Add("strFatura", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))

        Response.Redirect("CustoFaturaData.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strHolding", lblHolding.Text)
        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strCodFornecedorServico", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(9).Text))
        Session.Add("strNumeroDocumento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))
        Session.Add("strDataDocumento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(4).Text))
        Session.Add("strFatura", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))

        Response.Redirect("CustoFaturaData.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Excluir

        Session.Add("strHolding", lblHolding.Text)
        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strCodFornecedorServico", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(9).Text))
        Session.Add("strNumeroDocumento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))
        Session.Add("strDataDocumento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))
        Session.Add("strFatura", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(3).Text))

        Response.Redirect("CustoFaturaData.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("CustoDeImportacao.aspx")

    End Sub

    Protected Sub ddlCompanhia_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCompanhia.SelectedIndexChanged

        FillDDLEstabelecimento()

    End Sub

    Protected Sub BtnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConsultar.Click

        Session.Add("strOperacao", "C") ' Consultar

        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strFatura", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Response.Redirect("FaturaData.aspx")

    End Sub

    Protected Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnItem.Click

        'Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodigoDaHolding", lblHolding.ToolTip)
        Session.Add("strCodigoDaCia", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("strCodigoDoEstabelecimento", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("strFatura", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Response.Redirect("FaturaLinha.aspx")

    End Sub

End Class
